using System;
using System.Xml;
using System.Collections;

using ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Property;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowDataTypes;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity
{
	//*********************************************************************
	//
	// EntityAttribute Class
	//
	// The EntityAttribute class represents an attribute associated to an
	// entity width his prroperties
	//
	//*********************************************************************
	public class XMLEntityAttribute : bOSdotNET.XML.Data.XMLObject
	{
		/** PRIVATE FIELDS **/

		public const string MAIN_NODE	= "Attribute";

		private const string NAME_ATTRIBUTE			= "Name";
		private const string ALIAS_ATTRIBUTE			= "Alias";
		private const string ENTITYTYPE_ATTRIBUTE	= "EntityType";
		private const string ENTITYREF_ATTRIBUTE	= "EntityRef";
		private const string ORDER_ATTRIBUTE		= "Order";
		private const string OBJECTDATA_ATTRIBUTE	= "ObjectData";
		private const string FILTERCONDITION_ATTRIBUTE	= "FilterCondition";
		private const string DBFIELD_PARAMETER			= "DBField";


		private const string VALUE_NODE	= "Value";
		private const string TYPE_NODE	= "Type";

		public const string ENTITYTYPE_MAINENTITY	= "MainEntity";
		public const string ENTITYTYPE_SUBENTITY	= "SubEntity";
		public const string ENTITYTYPE_NOENTITY	= "NoEntity";

		public const string ENTITYREF_NONE		= "NONE";
		public const string ORDER_NONE			= "NONE";
		public const string OBJECTDATA_NONE	= "NONE";

		public const string TYPE_DB			= "DB";
		public const string TYPE_TRADING		= "Trading";
		public const string TYPE_TECHNICALIND	= "Technical-Ind";
		public const string TYPE_TECHNICALDEP	= "Technical-Dep";
		public const string TYPE_SETTINGIND	= "Setting-Ind";
		public const string TYPE_SETTINGDEP	= "Setting-Dep";


		private string sName;
		private string sAlias;
		private string sEntityType;
		private string sEntityRef;
		private string sOrder;
		private string sObjectData;
		private string sValue;
		private string sType;

		private string sFilterCondition;
		private string sDBField;

		private XMLProperties vProperties;
		private XMLDataType xmlDataType;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLEntityAttribute()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLEntityAttribute(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		public XMLEntityAttribute(string _Name)
		{
			this.Init();

			this.Name = _Name;
		}

		override public void Init()
		{
			sName = string.Empty;
			sEntityType= ENTITYTYPE_NOENTITY;
			sEntityRef= ENTITYREF_NONE;
			sOrder= ORDER_NONE;
			sObjectData= OBJECTDATA_NONE;
			sValue = string.Empty;
			sType = string.Empty;
			sAlias = string.Empty;

			sFilterCondition = string.Empty;
			sDBField = string.Empty;

			vProperties = new XMLProperties();
			xmlDataType = new XMLDataType();
		}

		public XMLProperties XMLProperties 
		{
			get { return vProperties; }
			set { vProperties = value; }
		}

		public XMLDataType XMLDataType
		{
			get { return xmlDataType;}
			set { xmlDataType = value;}
		}

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Alias 
		{
			get { if ( sAlias.CompareTo (string.Empty) != 0) return sAlias; else return sName; }
			set { sAlias = value; }
		}

		public string EntityType 
		{
			get { return sEntityType; }
			set { sEntityType = value; }
		}

		public string EntityRef 
		{
			get { return sEntityRef; }
			set { sEntityRef = value; }
		}

		public string Order 
		{
			get { return sOrder; }
			set { sOrder = value; }
		}

		public string ObjectData 
		{
			get { return sObjectData; }
			set { sObjectData = value; }
		}

		public string Value 
		{
			get { return sValue; }
			set { sValue = value; }
		}

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string FilterCondition
		{
			get { return sFilterCondition; }
			set { sFilterCondition = value; }
		}

		public string DBField
		{
			get { return sDBField; }
			set { sDBField = value; }
		}

		/** INSTANCE METHODS **/

		/** Parsering method for filling the attribute **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLEntityAttribute.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;

					case XMLEntityAttribute.ALIAS_ATTRIBUTE:
						sAlias = node.InnerText;
						break;

					case XMLEntityAttribute.ENTITYTYPE_ATTRIBUTE:
						sEntityType = node.InnerText;
						break;
					case XMLEntityAttribute.ENTITYREF_ATTRIBUTE:
						sEntityRef = node.InnerText;
						break;
					case XMLEntityAttribute.ORDER_ATTRIBUTE:
						sOrder = node.InnerText;
						break;
					case XMLEntityAttribute.OBJECTDATA_ATTRIBUTE:
						sObjectData = node.InnerText;
						break;

					case XMLEntityAttribute.FILTERCONDITION_ATTRIBUTE:
						this.FilterCondition = node.InnerText;
						break;

					case XMLEntityAttribute.DBFIELD_PARAMETER:
						this.DBField = node.InnerText;
						break;

					case XMLEntityAttribute.VALUE_NODE:
						this.Value = node.InnerText;
						break;

					case XMLEntityAttribute.TYPE_NODE:
						this.Type = node.InnerText;
						break;
				}
			}

			/** find the sub tags witch contains EntityAttribute values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLEntityAttribute.VALUE_NODE:
						sValue = node.InnerText;
						break;
					case XMLEntityAttribute.TYPE_NODE:
						sType=node.InnerText;
						break;

					/** define the subcollection of the Atributeproperties By parsering the subtag Properties **/
					case XMLProperties.MAIN_NODE:
						vProperties.Parse(node);
						break;

					case XMLDataType.MAIN_NODE:
						xmlDataType.Parse (node);
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLEntityAttribute.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.ALIAS_ATTRIBUTE, this.Alias ) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.ENTITYTYPE_ATTRIBUTE, sEntityType) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.ENTITYREF_ATTRIBUTE, sEntityRef) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.ORDER_ATTRIBUTE, sOrder) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.OBJECTDATA_ATTRIBUTE, sObjectData) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.FILTERCONDITION_ATTRIBUTE, this.sFilterCondition) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.DBFIELD_PARAMETER, this.DBField) );

			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLEntityAttribute.VALUE_NODE, sValue) );
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLEntityAttribute.TYPE_NODE, sType) );

			xmlNode.AddNode ( vProperties.GetXMLNode() );
			xmlNode.AddNode ( xmlDataType.GetXMLNode() );
			
			return xmlNode;
		}

		public bOSdotNET.XML.XMLNode GetShortXML()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLEntityAttribute.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLEntityAttribute.VALUE_NODE, sValue) );

			if ( vProperties.Count > 0 )
				xmlNode.AddNode ( vProperties.GetXMLNode() );

			if  ( ( xmlDataType.Key.CompareTo ( string.Empty ) != 0 ) || (xmlDataType.Type.CompareTo (string.Empty) != 0) )
				xmlNode.AddNode ( xmlDataType.GetXMLNode() );
			
			return xmlNode;
		}
	}
}